/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.material.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.pitop.material.api.MaterialCategoryFeignApi;
import com.pitop.core.exceptions.MaterialCategoryException;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.entity.Page;
import com.pitop.material.entity.MaterialCategory;


/**
 * 供应类型
 *
 * @author berton
 */
@Service("materialCategorySVImpl")
@Slf4j
public class MaterialCategorySVImpl {

    @Autowired
    private MaterialCategoryFeignApi materialCategoryFeignApi;

    /**
     * 加载一个对象MaterialCategory
     *
     * @param id   *
     * @param code 编码
     * @return MaterialCategory
     */
    public MaterialCategory load(Long id, String code) {
        if (id == null && code == null) {
            throw new MaterialCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialCategoryFeignApi.load(id, code);
    }

    /**
     * 加载一个对象MaterialCategory 通过id
     *
     * @param id id
     * @return MaterialCategory
     */
    public MaterialCategory loadById(Long id) {
        if (id == null) {
            throw new MaterialCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialCategoryFeignApi.loadById(id);
    }

    /**
     * 加载一个对象MaterialCategory 通过code
     *
     * @param code 编码
     * @return MaterialCategory
     */
    public MaterialCategory loadByCode(String code) {
        if (code == null) {
            throw new MaterialCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialCategoryFeignApi.loadByCode(code);
    }


    /**
     * 删除对象MaterialCategory
     *
     * @param id   id
     * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new MaterialCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        materialCategoryFeignApi.delete(id, code);
    }


    /**
     * 查询MaterialCategory分页
     *
     * @param materialCategory 对象
     * @param curPage          当前页
     * @param pageSize         分页条数
     * @return List<MaterialCategory>
     */
    public List<MaterialCategory> list(MaterialCategory materialCategory, int curPage, int pageSize) {
        return materialCategoryFeignApi.list(materialCategory, curPage, pageSize);
    }

    /**
     * 查询MaterialCategory分页
     *
     * @param id
     * @param code     编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<MaterialCategory>
     */
    public List<MaterialCategory> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        MaterialCategory materialCategory = new MaterialCategory();
        materialCategory.setId(id);
        materialCategory.setCode(code);

        return materialCategoryFeignApi.list(materialCategory, curPage, pageSize);
    }


    public int count(Long id, String code) {
        return materialCategoryFeignApi.count(id, code);
    }


    public int count(MaterialCategory materialCategory) {
        return materialCategoryFeignApi.count(materialCategory);
    }


    /**
     * 保存
     * 1.数据合法性验证
     * ****数据非空验证
     * ****供应类型名称是否已经存在
     * 2.保存供应类型
     *
     * @param materialCategory 实体
     */
    public void save(MaterialCategory materialCategory) {
        //1.数据合法性验证
        if (materialCategory == null) {
            throw new MaterialCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //供应类型名称是否已经存在
        MaterialCategory materialCategoryFlag = materialCategoryFeignApi.loadByName(materialCategory.getName());
        if (materialCategoryFlag != null) {
            throw new MaterialCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.保存供应类型
        materialCategoryFeignApi.build(materialCategory);
    }

    /**
     * 更新
     * 1.数据合法性验证
     * ****数据非空验证
     * ****供应类型名称是否已经存在
     * 2.更新供应类型
     *
     * @param materialCategory 实体
     */
    public void modify(MaterialCategory materialCategory) {
        //1.数据合法性验证
        if (materialCategory == null) {
            throw new MaterialCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //供应类型名称是否已经存在
        MaterialCategory materialCategoryFlag = materialCategoryFeignApi.loadByName(materialCategory.getName());
        if (materialCategoryFlag != null && !materialCategory.getCode().equals(materialCategoryFlag.getCode())) {
            throw new MaterialCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.更新供应类型
        materialCategoryFeignApi.modify(materialCategory);
    }

}
